﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using PracticeAzure.Website.Shared;
using System.Text;
using System.Web.Mvc;

namespace PracticeAzure.Website.Data
{
    public class CorrigendumReply
    {
        public long ID { get; set; }
        public long CorrigendumID { get; set; }
        public string Body { get; set; }
        public DateTime CreatedOn { get; set; }
        public string CreatedByEmail { get; set; }

        private string _createdByAvatar;
        public string CreatedByAvatar
        {
            get
            {
                if (_createdByAvatar == null)
                {
                    var md5 = CreatedByEmail.CalculateMD5();
                    if (string.IsNullOrWhiteSpace(md5))
                    {
                        _createdByAvatar = string.Empty;
                    }
                    else
                    {
                        _createdByAvatar = string.Format("http://www.gravatar.com/avatar/{0}?d=mm&s=40", md5);
                    }
                }
                return _createdByAvatar;
            }
        }

        public MvcHtmlString RenderedBody
        {
            get
            {
                var html = Body;
                html = RenderEngine.Current.Render(html);
                return MvcHtmlString.Create(html);
            }
        }

        public void Reply()
        {
            using (var conn = SqlHelperLite.CreateSqlConnection())
            {
                var cmd = conn.CreateCommand();
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = "ReplyCorrigendum";
                cmd.Parameters.Add(new SqlParameter("@id", CorrigendumID));
                cmd.Parameters.Add(new SqlParameter("@body", Body));
                if (string.IsNullOrWhiteSpace(CreatedByEmail))
                {
                    cmd.Parameters.Add(new SqlParameter("@email", DBNull.Value));
                }
                else
                {
                    cmd.Parameters.Add(new SqlParameter("@email", CreatedByEmail));
                }
                conn.Open();
                cmd.ExecuteNonQuery();
            }
        }

        public static IEnumerable<CorrigendumReply> List(long corrigendumId, int? page)
        {
            var replies = new List<CorrigendumReply>();
            using (var conn = SqlHelperLite.CreateSqlConnection())
            {
                var cmd = conn.CreateCommand();
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = "ListCorrigendumReplies";
                cmd.Parameters.Add(new SqlParameter("@corrigendumId", corrigendumId));
                cmd.Parameters.Add(new SqlParameter("@pageIndex", page.GetValueOrDefault(1)));
                cmd.Parameters.Add(new SqlParameter("@recordsPerPage", GlobalConfiguration.PageSize));
                conn.Open();
                using (var reader = cmd.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        replies.Add(new CorrigendumReply()
                        {
                            ID = (long)reader["ID"],
                            CorrigendumID = corrigendumId,
                            Body = (string)reader["Body"],
                            CreatedOn = (DateTime)reader["CreatedOn"],
                            CreatedByEmail = reader["CreatedByEmail"] == DBNull.Value ? string.Empty : (string)reader["CreatedByEmail"]
                        });
                    }
                }

            }
            return replies;
        }

        public static void Hide(long id)
        {
            using (var conn = SqlHelperLite.CreateSqlConnection())
            {
                var cmd = conn.CreateCommand();
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = "HideCorrigendum";
                cmd.Parameters.Add(new SqlParameter("@id", id));
                conn.Open();
                cmd.ExecuteNonQuery();
            }
        }
    }
}